<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/01/10
 * Time: 09:31 PM
 */

namespace Home\Controller;


use Think\Controller;

class AdminController extends Controller
{

    public function __construct()
    {
        parent::__construct();
        if (is_null($_SESSION["ADMIN_PLUGIN"])) {
            echo '<script> alert("请先登录")</script>';
            $this->redirect("/Login/login");
        }
    }
    public function index(){
        $this->display('Admin/index');
    }

    public function userlist(){

        $count=M("userinfo")->count();
        $page=new \Think\Page($count,10);
        $list=M("userinfo")->where('adminid= "'.$_SESSION['ADMIN_PLUGIN']['id'].'"')->limit($page->firstRow.','.$page->listRows)->select();
        $show=$page->show();

        $this->assign("userlist", $list);
        $this->assign("show", $show);
        $this->display('Admin/userlist');
    }

    public function zsuserlist($type){
        $list=M("userinfo")->where('adminid = '.$_SESSION['ADMIN_PLUGIN']['id'].' and type="'.$type.'"')->select();
        $this->assign("userlist", $list);
//        $this->assign("show", $show);
        $this->display('Admin/userlist');
    }

    public function useradd(){
        $this->display("Admin/user-add");
    }

    public function useradded(){
        $date['username'] = $_POST['username'];
        if (M('userinfo')->where('username= "'. $date['username'].'"')->find())
        {
            //已经注册过了
            $this->assign("addtype",2);
        }
        $date['password'] = md5($_POST['password']);
        $date['signature'] = $_POST['signature'];
        $date['time']=strtotime('+'.$_POST['time'].' day');
        $date['type']=$_POST['type'];
	    $date['rid']='0';
        $date['adminid']=$_SESSION['ADMIN_PLUGIN']['id'];
        M('userinfo')->add($date);
        $this->assign('addtype',1);
        $this->display("Admin/user-add");
    }


    public function useredit($id)
    {
        $user= M('userinfo')->where('id='.$id)->find();
        $this->assign('user',$user);
        $this->display('user-edit');
    }

    public function saveuser(){
        $id = $_POST['id'];
        $time = $_POST['time'];

        $user = M('userinfo')->where('id= "'. $id.'"')->find();
        $user['time'] =strtotime('+'.$time.' day', $user['time']);
        $user['signature'] = $_POST['signature'];
        $user['type']=2;
        M('userinfo')->save($user);

        $this->assign('addtype',1);
        $this->display('user-edit');
    }

    /**
     * 检查用户名是否存在
     * @param $param
     */
    public function usercheck($param)
    {

        if(M('userinfo')->where("username='".$param."'")->find())
        {
            echo "用户名已经存在";
        }else{
            echo "y";
        }
    }



    public function userpwd($id)
    {
        $this->assign("userid",$id);
        $this->display('userpassword');
    }


    public function userpw($id)
    {
        $data['password']=md5($_POST['password']);
        M('userinfo')->where('id='.$id)->save($data);
        $this->assign('addtype',1);
        $this->display('userpassword');
    }


    public function deluser($id)
    {
        M('userinfo')->where('id='.$id)->delete();
        $this->userlist();
    }

    public function logout()
    {
        $_SESSION["ADMIN"] = null;
        $this->redirect("/Login/login");
    }


    public function system(){
        $this->assign('sys',M('sys')->find());
        $this->display('system');
    }

    public function setsys(){


        $sys = M('sys')->find();
        $sys['version'] = $_POST['version'];
        $sys['address']=$_POST['address'];

        if( $_POST['type'])
        {
            $sys['type']=1;
        }else{
            $sys['type']=0;
        }
        M('sys')->save($sys);
        $this->assign('sys',M('sys')->find());
        $this->assign('addtype',1);
        $this->display('system');

    }

    public function searchuser(){
        $name = $_GET['name'];
        $list = M('userinfo')->where("adminid = ".$_SESSION['ADMIN_PLUGIN']['id']." and username like '%".$name."%'")->select();
        $this->assign("userlist", $list);
        $this->display('Admin/userlist');
    }

    public function timecard(){
        $cardlist = M('timecard')->select();

        foreach($cardlist  as &$item){

            if($item['user']==-1)
            {
                $item['user']='未使用';
            }else {
                $item['user'] = $this->getUserNameById($item['user']);
            }
        }
        $this->assign('cardlist',$cardlist);
        $this->display('timecard');
    }

    public function getUserNameById($id){
        $user = M('userinfo')->where('id = "'.$id.'"')->find();
            return $user['username'];
    }

    public function addCard(){
        $time = $_GET['time'];
        $num = $_GET['num'];
        $i=0;
        while($i<$num){
            $output=time()*2;
                for ($a = 0; $a < 7; $a++) {
                    $output .= chr(mt_rand(48, 57));    //生成php随机数
                }
            $output = $output.$i;
            $card['carid'] = $output;
            $card['user']=-1;
            $card['time']=$time;
            M('timecard')->add($card);
            $i++;
        }
        $this->redirect('Admin/timecard');
    }
}
